package cim3d;

import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2009-10-24
 */
public class DefaultKeyListener implements KeyListener {
    GraphDisplayer displayer;

    public DefaultKeyListener() {
    }

    public DefaultKeyListener(GraphDisplayer gl) {
        this.displayer = gl;
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
        if(displayer == null)
            return;
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                displayer.getViewerData().setYp(displayer.getViewerData().getYp() + displayer.getViewerData().getMove());
                break;
            case KeyEvent.VK_DOWN:
                displayer.getViewerData().setYp(displayer.getViewerData().getYp() - displayer.getViewerData().getMove());
                break;
            case KeyEvent.VK_LEFT:
                displayer.getViewerData().setXp(displayer.getViewerData().getXp() + displayer.getViewerData().getMove());
                break;
            case KeyEvent.VK_RIGHT:
                displayer.getViewerData().setXp(displayer.getViewerData().getXp() - displayer.getViewerData().getMove());
                break;
            case KeyEvent.VK_HOME:
                displayer.getViewerData().setZp(displayer.getViewerData().getZp() - displayer.getViewerData().getMove());
                break;
            case KeyEvent.VK_END:
                displayer.getViewerData().setZp(displayer.getViewerData().getZp() + displayer.getViewerData().getMove());
                break;
            case KeyEvent.VK_PAGE_UP:
                if (Math.abs(displayer.getViewerData().getXrot()) > 0.1)
                    displayer.getViewerData().setXrot(displayer.getViewerData().getXrot() + 15);
                break;
            case KeyEvent.VK_PAGE_DOWN:
                if (Math.abs(displayer.getViewerData().getXrot() + 60.0) > 0.1)
                    displayer.getViewerData().setXrot(displayer.getViewerData().getXrot() - 15);
                break;
            case KeyEvent.VK_F5:
                displayer.getViewerData().setZrot(displayer.getViewerData().getZrot() + 45);
                break;
            case KeyEvent.VK_F6:
                displayer.getViewerData().setZrot(displayer.getViewerData().getZrot() - 45);
                break;
        }
    }

    public void keyReleased(KeyEvent e) {
    }

    public GraphDisplayer getDisplayer() {
        return displayer;
    }

    public void setDisplayer(GraphDisplayer displayer) {
        this.displayer = displayer;
    }
}
